package com.xiao.framework.ds.autoconfigure.factory;

import com.xiao.framework.ds.autoconfigure.bean.DataSourceBean;
import com.xiao.framework.ds.autoconfigure.bean.MultiDataSourceProperties;
import com.xiao.framework.ds.autoconfigure.builder.DataSourceBuilder;
import java.util.HashMap;
import java.util.Map;
import javax.sql.DataSource;

/**
 * @author <a href="mailto:xiaoquanidea@163.com">hutiquan</a>
 * @since 2019-08-24 9:43
 */
public class DefaultMultiDataSourceFactory implements MultiDataSourceFactory {
  private final MultiDataSourceProperties multiDataSourceProperties;
  private final DataSourceBuilder<? extends DataSource> dataSourceBuilder;

  public DefaultMultiDataSourceFactory(MultiDataSourceProperties multiDataSourceProperties,
      DataSourceBuilder<? extends DataSource> dataSourceBuilder) {
    this.multiDataSourceProperties = multiDataSourceProperties;
    this.dataSourceBuilder = dataSourceBuilder;
  }

  @Override
  public Map<String, DataSource> loadDataSource() {
    Map<String,DataSource> dataSourceMap = new HashMap<>();
    Map<String, DataSourceBean> dataSourceBeanMap = multiDataSourceProperties.getDataSource();
    dataSourceBeanMap.forEach((key, dataSourceBean)->{
      dataSourceMap.put(key,dataSourceBuilder.buildDataSource(key,dataSourceBean));
    });
    return dataSourceMap;
  }

}
